<template>
    <div id="login-page">
      <div class="login-form" v-if="createSuccess === 'login'">
        <el-form ref="loginForm"
                 :model="loginForm"
                 :rules="rules"
                 autocomplete="new-password">
          <div class="login-title">Login</div>
          <el-form-item prop="username">
            <input type="text"
                   name="username"
                   style="display:none" />
            <el-input v-model="loginForm.username"
                      type="text"
                      name="username"
                      auto-complete="off"
                      placeholder="username">
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <input type="password"
                   name="inputDom"
                   style="display:none" />
            <el-input v-model="loginForm.password"
                      type="password"
                      name="inputDom"
                      auto-complete="new-password"
                      placeholder="password">
            </el-input>
          </el-form-item>
          <el-form-item>
            <el-button @click="submitForm('loginForm')"
                       class="login-btn"
                       type="primary">Login</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
</template>

<script>
import { mapActions } from 'vuex'
export default {
  data() {
    return {
      createSuccess: 'login',
      tipText: '',
      loginForm: {
        username: '',
        password: ''
      },
      rules: {
        password: [
          { required: true, message: 'password', trigger: 'blur' }
          // { validator: checkPassword, trigger: 'blur' }
        ],
        username: [
          { required: true, message: 'username', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    // createSuccess() {

    // }
  },
  methods: {
    ...mapActions(['getLoginData']),
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.getLoginData(this)
        } else {
          return false
        }
      })
    },
    enterToSubmit(event) {
      if (event.which === 13) this.submitForm('loginForm')
    },
  },
  mounted() {
    this.tipText = this.createSuccess === 'hotel' ? '创建酒店成功，3s后进入酒店管理平台' : '创建集团成功，3s后进入集团管理平台'
    document.addEventListener('keydown', this.enterToSubmit)
  },
  beforeRouteLeave(to, from, next) {
    document.removeEventListener('keydown', this.enterToSubmit)
    next()
  }
}
</script>

<style lang="less" scoped>
#login-page {
  position: relative;
  height: 100%;
}

.forget-pass {
  display: block;
  text-align: right;
  font-size: 13px;
  left: 250px;
  color: #029ae5;
  cursor: pointer;
}
.login-form {
  position: absolute;
  top: 50%;
  left: 50%;
  margin-left: -235px;
  margin-top: 183px;
  width: 470px;
  height: 367px;
  box-sizing: border-box;
  padding: 35px 60px;
  padding-top: 0;
  background: #fff;
  border-radius: 3px;
}
.success-form {
  height: 346px;
  text-align: center;
  h3 {
    position: relative;
    font-size: 20px;
    top: 100px;
  }
  .success-btn {
    position: relative;
    top: 130px;
  }
}
.login-title {
  padding: 16px 0;
  font-size: 24px;
  text-align: center;
}

.login-btn {
  width: 100%;
  border-radius: 16px;
}
.sign-up {
  color: #029ae5;
  cursor: pointer;
}
</style>
